package cn.dglydrpy.study.algorithm.od;

/**
 * @author Yyy
 * @date 2024/11/6 19:43
 * @description
 * 题目描述：输入由小写字母组成的字符串，字符串首相连成一环形，找出所有字符都出现偶数次的最长子字符串的长度。
 * i o x  偶数次（包括0）
 * 环形字符串
 * 
 输入：alolobo   looxdolx     bcbcbc
 输出：6         7            6
 */              
public class Od0014_LongestSubStr {
    public static void main(String[] args) {

//        Scanner scanner = new Scanner(System.in);
//        while(scanner.hasNextLine()){
//            String input = scanner.nextLine();
//
//            int ans = myAnswer(input);
//            System.out.println(ans);
//            int answer1 = answer1(input);
//            System.out.println(answer1);
//        }

        // 对数器验证
        int testTime = 10000;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            String s = genS();
//            System.out.println(s);
            int answer1 = answer1(s);
            int myAnswer = myAnswer(s);
            if(answer1 != myAnswer){
                succeed = false;
                System.out.println(answer1);
                System.out.println(myAnswer);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
        
    }

    private static String genS() {
        StringBuilder stringBuilder = new StringBuilder();
        int length = (int) (Math.random()*100) + 1;
        while(length > 0){
            int random = (int) (Math.random()*26);
            char c = (char) ('a' + random);
            stringBuilder.append(c);
            length--;
        }
        String s = stringBuilder.toString();
        return s;
    }

    private static int myAnswer(String input) {
        int ans = 0;
        
        if(input == null){
            return ans;
        }

        int n = input.length();
        input += input;
        
        for(int i = 0;i<n;i++){
            // 考察以i号位置开始的字符串
            
            for(int j = i;j<n + i;j++){ // substring含头不含尾
                String sub = input.substring(i,j+1);
//                System.out.println(sub);
                if(check(sub)){
                    ans = Math.max(ans,sub.length());
//                    System.out.println(sub);
                }
            }
        }
        return ans;
    }

    private static boolean check(String sub) {
        int icount = 0;
        int ocount = 0;
        int xcount = 0;
        char[] chars = sub.toCharArray();
        for(int i = 0;i<chars.length;i++){
            if(chars[i] == 'i'){
                icount++;
            }else if(chars[i] == 'o'){
                ocount++;
            }else if(chars[i] == 'x'){
                xcount++;
            }
        }
        return icount%2==0  && ocount%2==0 && xcount%2 == 0;
    }


    public static int answer1(String a) {
        String src = a + a;
        int resultlength = a.length();
        int ICount = 0;
        int oCount = 0;
        int xCount = 0;
        int length = 0;
        out:
        for (int i = resultlength; i > 0; i--) {
            if (resultlength == 1) {
                length = 0;
//                break;
            }
            for (int j = 0; j < a.length(); j++) {
                String currentString = src.substring(j, j + i);
                String currento = currentString.replaceAll("o", "");
                String currentl = currentString.replaceAll("i", "");
                String currentx = currentString.replaceAll("x", "");
                if ((currentString.length() - currento.length()) % 2 == 0 && (currentString.length() - currentl.length()) % 2 == 0 && (currentString.length() - currentx.length()) % 2 == 0) {
                    length = i;
                    break out;
                }
                if(resultlength == i){
                    break;
                }
            }
        }
        return length;
    }
}
